package routers

import (
	"awesome/models"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func InitUser() {

	Api.GET("/user/:id", func(c *gin.Context) {
		id := c.Param("id")
		var user *models.User
		var idu, err = strconv.ParseUint(id, 10, 64)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
			return
		}
		user, _ = models.GetUserByID(uint(idu))
		c.JSONP(http.StatusOK, user)
	})

	Api.GET("/user", func(c *gin.Context) {
		email := c.Query("email")
		var user *models.User
		user, err := models.GetUserByEmail(email)
		if err != nil {
			println(err)
		}
		c.JSONP(http.StatusOK, user)
	})

	Api.POST("/user", func(c *gin.Context) {
		var user *models.User
		err := c.BindJSON(&user)
		if err != nil {
			c.JSONP(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
		println(user)
		err = models.CreateUser(user)
		if err != nil {
			return
		}
		c.JSONP(http.StatusOK, user)
	})
}
